home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / userconf / shadow.c < prev    next >
C/C++ Source or Header  |  1996-07-21  |  3KB  |  134 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include "internal.h"
  6. #include "userconf.h"
  7. #include "../paths.h"
  8. #include "userconf.m"
  9.  
  10. #define ETC_SHADOW_TMP    "/etc/shadow.tmp"
  11. static USERCONF_HELP_FILE help_shadow ("shadow");
  12. static CONFIG_FILE f_shadow (ETC_SHADOW,help_shadow,CONFIGF_MANAGED
  13.     ,"root","root",0600);
  14.  
  15.  
  16.  
  17. /*
  18.     Used when reading the /etc/shadow file
  19. */
  20. PUBLIC SHADOW::SHADOW(const char *line)
  21. {
  22.     char words[9][100];
  23.     user_splitline (line,words);
  24.     name.setfrom (words[0]);
  25.     passwd.setfrom (words[1]);
  26.     last = atoi(words[2]);
  27.     may = atoi(words[3]);
  28.     must = atoi(words[4]);
  29.     warn = atoi(words[5]);
  30.     expire = atoi(words[6]);
  31.     disable = atoi(words[7]);
  32.     reserved.setfrom(words[8]);
  33. }
  34. /*
  35.     Used when creating a new record
  36. */
  37. PUBLIC SHADOW::SHADOW()
  38. {
  39.     last = 0; 
  40.     may = 0; 
  41.     must = 0;
  42.     warn = 0;
  43.     expire = 0;
  44.     disable = 0;
  45. }
  46.  
  47. /*
  48.     Return the crypt password field
  49. */
  50. PUBLIC const char *SHADOW::getpwd()
  51. {
  52.     return passwd.get();
  53. }
  54. /*
  55.     Write one record of /etc/passwd
  56. */
  57. PUBLIC void SHADOW::write(FILE *fout)
  58. {
  59.     fprintf (fout,"%s:%s:%d:%d:%d:%d:%d:%d:%s\n"
  60.         ,name.get(),passwd.get(),last,may,must,warn,expire,disable
  61.         ,reserved.get());
  62. }
  63.  
  64. PUBLIC SHADOWS::SHADOWS()
  65. {
  66.     /* #Specification: /etc/passwd / strategy
  67.         /etc/shadow is read "by hand" instead of using getpwent() to avoid
  68.         getting all those NIS entries. This is done when editing local
  69.         user account.
  70.     */
  71.     FILE *fin = f_shadow.fopen ("r");
  72.     if (fin != NULL){
  73.         char line[1000];
  74.         while (fgets(line,sizeof(line)-1,fin)!=NULL){
  75.             strip_end (line);
  76.             if (line[0] != '\0'){
  77.                 add (new SHADOW(line));
  78.             }
  79.         }
  80.         fclose (fin);
  81.     }
  82.     rstmodified();
  83. }
  84. PUBLIC SHADOW *SHADOWS::getitem(int no)
  85. {
  86.     return (SHADOW*)ARRAY::getitem(no);
  87. }
  88. /*
  89.     Get one SHADOW specification of the table or NULL from his login name
  90. */
  91. PUBLIC SHADOW *SHADOWS::getitem(const char *name)
  92. {
  93.     SHADOW *ret = NULL;
  94.     int nbu = getnb();
  95.     for (int i=0; i<nbu; i++){
  96.          SHADOW *usr = getitem(i);
  97.         if (usr->name.cmp(name)==0){
  98.             ret = usr;
  99.             break;
  100.         }
  101.     }
  102.     return ret;
  103. }
  104.  
  105. PUBLIC int SHADOWS::write()
  106. {
  107.     int ret = -1;
  108.     FILE *fout = f_shadow.fopen (ETC_SHADOW_TMP,"w");
  109.     if (fout != NULL){
  110.         int nbu = getnb();
  111.         for (int i=0; i<nbu; i++){
  112.             getitem(i)->write(fout);
  113.         }
  114.         fclose(fout);
  115.         unlink(ETC_SHADOW ".OLD");
  116.         link(ETC_SHADOW, ETC_SHADOW ".OLD");
  117.         unlink(ETC_SHADOW);
  118.         link(ETC_SHADOW_TMP, ETC_SHADOW);
  119.         unlink(ETC_SHADOW_TMP);
  120.         ret = 0;
  121.     }
  122.     return ret;
  123. }
  124.  
  125. /*
  126.     Return != 0 if the /etc/shadow file exist
  127. */
  128. int shadow_exist ()
  129. {
  130.     return f_shadow.exist();
  131. }
  132.  
  133.  
  134.